﻿using System.Management.Automation;
using DotNet.DevOps.Tasks.AppConfig;
using DotNet.DevOps.Tasks.Util;

namespace DotNet.DevOps.Tasks.Cmdlets
{
    /// <summary>
    /// Updates or adds app settings to your app.config / web.config file(s).
    /// If no matching key is found the default behaviour is to add a new key. This can be overridden by setting AddIfMissing. 
    /// Also only 1 occourence of the key is expteced by default. This can be overridden by setting ExpectedMatches.
    /// 
    /// Default behaviour:
    /// * Adds key if missing.
    /// * Expects 1 key match.
    /// 
    /// </summary>
    /// <example>
    /// import-module .\DotNet.DevOps.Tasks.dll
    /// 
    /// Update-AppSetting -Property @{  Key             = "DataSource"
    ///                                 Value           = "TESTDB"
    ///                                 Files           = "app.config"
    ///                              }
    /// </example>
    [Cmdlet(VerbsData.Update, "AppSetting", ConfirmImpact = ConfirmImpact.Low, SupportsShouldProcess = false, SupportsTransactions = false)]
    public class Update_AppSetting : Cmdlet, IAppSetting
    {
        /// <summary>
        /// App setting Key to find.
        /// </summary>
        [Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string Key { get; set; }

        /// <summary>
        /// App setting Value to set.
        /// </summary>
        [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string Value { get; set; }

        /// <summary>
        /// File(s) to update.
        /// </summary>
        [Parameter(Mandatory = true, Position = 2, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string Files { get; set; }

        /// <summary>
        /// Default true, set to false if you want the build to fail if the key is not allready present in the file.
        /// </summary>
        [Parameter(Mandatory = false, Position = 3, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public bool AddIfMissing { get; set; }

        /// <summary>
        /// Number of matches that should be found in the file. If the number do not match the number of actual entries in the file, the build will fail.
        /// </summary>
        [Parameter(Mandatory = false, Position = 4, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public int ExpectedMatches { get; set; }

        private AppSetting task;

        public Update_AppSetting()
        {
            task = new AppSetting(new PowershellCmdletLogger(this));
            AddIfMissing = task.AddIfMissing;
            ExpectedMatches = task.ExpectedMatches;
        }

        protected override void BeginProcessing()
        {
            task = new AppSetting(new PowershellCmdletLogger(this))
                .CopyPropertyValuesFrom(this) as AppSetting;
        }

        protected override void ProcessRecord()
        {
            task.Execute();
        }
    }
}
